<!--基本信息编辑-->
<template>
    <div class="page_editbaseInfo">
        <common-header :title="'基本信息编辑'"></common-header>
        <div class="btn-save" @click="updateFamilyBaseInfo">保存</div>
        <div class="pages-main" style="height: calc( 100vh - 45px)">
            <div class="item-info">
                <div v-for="(item,index) in baseInfo" class="item-item">
                    <x-input v-if="item.label !== '户籍地'" :title="item.label" v-model="item.value"
                             :placeholder="item.placeholder"></x-input>
                    <div v-else class="item-item every-block-padding item-inline">
                        <span class="item-itemLabel">户籍地</span>
                        <select class="translateBlock" v-model="item.value" placeholder="请选择户籍地"
                                style="text-align-last: right;width: 7.6rem;font-size:0.38rem;">
                            <option v-for="item in xnsRegionList" :value="item.code">{{item.name}}</option>
                        </select>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
    import {XInput, Selector} from 'vux'
    import resident from '../../services/resident'

    export default {
        name: "editbaseInfo",
        components: {
            XInput,
            Selector
        },
        data() {
            return {
                familyid: Number(this.$route.query.familyid),
                baseInfo: [
                    {label: '户主姓名', 'key': 'name', value: this.$route.query.name, placeholder: '请输入户主姓名'},
                    {
                        label: '身份证号',
                        'key': 'idCardNumber',
                        value: this.$route.query.idCardNumber,
                        placeholder: '请输入身份证号'
                    },
                    {
                        label: '户口编号',
                        'key': 'familyNumber',
                        value: this.$route.query.familyNumber,
                        placeholder: '请输入户口编号'
                    },
                    {label: '户籍地', 'key': 'villageCode', value: this.$route.query.villageCode, placeholder: '请选择户籍地'},
                    {
                        label: '具体地址',
                        'key': 'houseAddress',
                        value: this.$route.query.houseAddress,
                        placeholder: '请输入具体地址'
                    },
                ],
                xnsRegionList: [],
            }
        },

        created() {

        },

        mounted() {
            this.getXnsRegionList()
        },
        watch: {},
        methods: {
            // 获取户籍地列表
            getXnsRegionList() {
                let _param = this.$tools.invokeUAF('resident/getXnsRegionList', {})
                resident.getXnsRegionList(_param).then(res => {
                    let data = res.data.data;
                    this.xnsRegionList = data;
                })
            },

            // 保存用户基本信息
            updateFamilyBaseInfo() {

                let param = {
                    name: '请输入户主姓名',
                    idCardNumber: '请输入身份证号',
                    familyNumber: '请输入户口编号',
                    villageCode: '请选择户籍地',
                    houseAddress: '请输入具体地址',
                }

                this.baseInfo.forEach((item, j) => {
                    for (let i in param) {
                        if (i === item.key) {
                            if (!item.value) {
                                this.$vux.toast.show({
                                    text: param[i],
                                    type: 'text',
                                })
                                return;
                            }
                        }
                    }
                })

                let _info = {
                    name: this.baseInfo[0].value,
                    idCardNumber: this.baseInfo[1].value,
                    familyNumber: this.baseInfo[2].value,
                    villageCode: this.baseInfo[3].value,
                    houseAddress: this.baseInfo[4].value,
                };

                if (this.familyid) {
                    _info = Object.assign(_info, {familyId: this.familyid,})
                    let _param = this.$tools.invokeUAF('resident/updateFamilyBaseInfo', _info)
                    resident.updateFamilyBaseInfo(_param).then(res => {

                        let data = res.data.data;
                        if (res.data.code === '0') {
                            this.$tools.goPage_query('accoutInfo', {familyid: this.familyid})
                        }
                    })
                } else {
                    let _param = this.$tools.invokeUAF('resident/addFamilyBaseInfo', _info)
                    resident.addFamilyBaseInfo(_param).then(res => {
                        let data = res.data.data;
                        if (res.data.code === '0') {
                            this.familyid = data.id;
                            this.$tools.goPage_query('accoutInfo', {familyid: this.familyid})
                        }
                    })
                }
            },
        },
    };
</script>

<style lang="less" rel="stylesheet/less" type="text/css">
    .page_editbaseInfo {
    }
</style>
